package lightcar;

import java.awt.Graphics;
import java.util.ArrayList;

public class World
{
    public ArrayList<Car> cars;
    public ArrayList<LightSource> lights;
    private double speed;
    
    World()
    {
        cars = new ArrayList<Car>();

        cars.add(new Car(new Point(300,300),0.0,0.0,20.0,20.0,0.0));//

        lights = new ArrayList<LightSource>();
        speed = 1;
    }
    
    public void moveAll()
    {
        for(int k = 0; k<cars.size(); k++)
        {
            cars.get(k).move(light_intensity(cars.get(k).leftSensor()), light_intensity(cars.get(k).rightSensor()), speed);
        }
    }
    
    public void draw(Graphics g)
    {
        for(int k = 0; k<cars.size(); k++)
        {
            cars.get(k).draw(g);
        }
        for(int k = 0; k<lights.size(); k++)
        {
            lights.get(k).draw(g);
        }
    }
    
    public double light_intensity(Point p)
    {
        double intensity = 0;
        for(int k = 0; k<lights.size(); k++)
        {
            intensity += lights.get(k).get_intensity(p);
        }
        return intensity;
    }
    
    public void reset()
    {
        cars = new ArrayList<Car>();
        lights = new ArrayList<LightSource>();
    }
    
    public static double dist(Point a, Point b)
    {
        return Math.sqrt(Math.pow(a.x-b.x,2) + Math.pow(a.y-b.y,2));
    }
    
    public static double direction(Point a, Point b)
    {
        //Returns the angle of a vector defined by origin 'a' and endpoint 'b'
        double result = Math.toDegrees(Math.atan2((a.y-b.y),(b.x-a.x)));
        if(result < 0)
        {
            return result + 360;
        }
        else
        {
            return result;
        }
    }
}
